import xml.dom.minidom as dom
import os.path
import string
import sys
sys.path.append("temp/database")
import tarfile

data = tarfile.TarFile("data.pak","r")
data.extract("database/mysql.py","temp")
data.extract("database/mysql_config.cfg","temp")
data.extract("database/sqlite.py","temp")
db = ""
try:
    import mysql
    db = "mysql"
except:
    import sqlite
    db = "sqlite"
os.remove("temp/database/sqlite.py")
os.remove("temp/database/mysql.py")
os.remove("temp/database/mysql_config.cfg")
user = ""

class suser:
    def __init__(self):
        pass
    def setuser(self, usr):
        global user
        user = usr

class Error:
    
    def __init__(self, me):
        self._str = me
        
    def report(self):
        print self._str
        
class save:
    def __init__(self):
        pass
    def save(self, obj, class_):
        pass

class bestand:
    
    def __init__(self, name=''):
        self.reset()
        self._name = name
       
    def reset(self):
        self.name = ''
        self._beschreibung = ''

    def beschreibung(self):
        return self._beschreibung.encode('utf-8')

class person(bestand, save):
    def __init__(self, name):
        bestand.__init__(self, name)

    def lade(self, path):
        self._fragen = []
        self._antworten = []
        
        data.extract('personen/'+string.lower(self._name)+'.xml', 'temp')
        
        m_path = os.path.join(path, 'temp/personen/'+string.lower(self._name)+'.xml')
        b = dom.parse(m_path)
        os.remove('temp/personen/'+string.lower(self._name)+'.xml')
        
        self._name = b.getElementsByTagName('name')[0].firstChild.data.strip()
        self._beschreibung = b.getElementsByTagName('beschreibung')[0].firstChild.data.strip()
        
        for z in b.getElementsByTagName('frage'):
            fr = str(z.firstChild.data.strip())
            self._fragen.append(fr)
        for z in b.getElementsByTagName('antwort'):
            an = str(z.firstChild.data.strip())
            self._antworten.append(an)


    def frage(self):
        f = 1
        for n in self._fragen:
            print str(f) + " - " + n
            f = f + 1
        a = int(input("Was willst du fragen? (Zahl eingeben)"))
        a = a - 1
        print self._antworten[a]

    def beschreibung(self):
        return self._beschreibung + ', ' + self._name
    
class ort(bestand, save):
    
    def __init__(self, name):
        bestand.__init__(self, name)
    
    def lade(self, path):
        global data
        
        self._werkzeuge = []
        self._maschinen = []
        self._botschaften = []
        self._links = []
        self._personen = []
        self._notiz = []
        
        data.extract('orte/'+string.lower(self._name)+'.xml', 'temp')
        
        m_path = os.path.join(path, 'temp/orte/'+string.lower(self._name)+'.xml')
        baum = dom.parse(m_path)
        os.remove('temp/orte/'+string.lower(self._name)+'.xml')
        
        self._name = baum.getElementsByTagName('name')[0].firstChild.data.strip()
        self._beschreibung = baum.getElementsByTagName('beschreibung')[0].firstChild.data.strip()
        
        for w in baum.getElementsByTagName('werkzeug'):
            we = werkzeug(w.firstChild.data.strip())
            we.lade(path)
            self._werkzeuge.append(we)
            
        for m in baum.getElementsByTagName('maschine'):
            ma = maschine(m.firstChild.data.strip())
            ma.lade(path)
            self._maschinen.append(ma)
            
        for b in baum.getElementsByTagName('botschaft'):
            bo = botschaft(b.firstChild.data.strip())
            bo.lade(path)
            self._botschaften.append(bo)
            
        for L in baum.getElementsByTagName('link'):
            Li = link(L)
            self._links.append(Li)

        for p in baum.getElementsByTagName('person'):
            pe = person(p.firstChild.data.strip())
            pe.lade(path)
            self._personen.append(pe)

        self._notiz = baum.getElementsByTagName('notizblock')[0].firstChild.data.strip()


    def beschreibung(self):

        t = ''
        kk = []
        LL = []
        pp = ''
        for n in self._links:
            for k in self._maschinen:
                if n._anker == k._name:
                    t = t + n.beschreibung(k)
                    kk.append(k._name)
                    LL.append(n._richtung)
        for k in self._maschinen:
            if k._name not in kk:
                t = t + n.beschreibung(k)
        for n in self._links:
            if n._richtung not in LL:
                t = t + n.beschreibung('')
        #for p in self._personen:
            
        b = ' '.join( [ n.beschreibung() for n in self._botschaften] )
        w = ' '.join( [ n.beschreibung() for n in self._werkzeuge] )
        p = ' '.join( [ n.beschreibung() for n in self._personen] )
        return self._beschreibung.encode('utf-8') +' '+ t + ' ' + b + ' ' + w + ' ' + p
    
    
    def gehe(self, richtung):
        for n in self._links:
            if n._richtung == richtung:
                if n._bedingung == '': return n.follow()
                else:
                    for m in self._maschinen:
                        if m.zustand() == n._bedingung:
                            return n.follow()
                        else:
                            raise Error, 'Zugang wird verwehrt.'
        raise Error, 'Kann nicht in '+richtung+' gehen.'

    def notiz(self):
        return self._notiz
    
    def help_me(self):
        return self.__doc__

    def save(obj):

        save.save(obj,'person')
    
class botschaft(bestand, save):

    def lade(self, path):
        
        self._inhalt = ''
        self.punkte = 0
        
        
        data.extract('botschaften/'+string.lower(self._name)+'.xml', 'temp')
        
        m_path = os.path.join(path, 'temp/botschaften/'+string.lower(self._name)+'.xml')
        bot = dom.parse(m_path)
        os.remove('temp/botschaften/'+string.lower(self._name)+'.xml')
        
        self._name = bot.getElementsByTagName('name')[0].firstChild.data.strip()
        self._beschreibung = bot.getElementsByTagName('beschreibung')[0].firstChild.data.strip()
        self._inhalt = bot.getElementsByTagName('inhalt')[0].firstChild.data.strip()
        self._punkte = int(bot.getElementsByTagName('punkte')[0].firstChild.data.strip())
        
    def content(self):
        return self._inhalt.encode('utf-8')

class werkzeug(bestand, save):
    


    def lade(self, path):
        global user
        self._tipp = ''
        self._verwendbarkeit = 0
        
        data.extract('werkzeuge/'+string.lower(self._name)+'.xml', 'temp')
        
        m_path = os.path.join(path, 'temp/werkzeuge/'+string.lower(self._name)+'.xml')
        baum = dom.parse(m_path)
        os.remove('temp/werkzeuge/'+string.lower(self._name)+'.xml')
        
        self._name = baum.getElementsByTagName('name')[0].firstChild.data.strip()
        self._beschreibung = baum.getElementsByTagName('beschreibung')[0].firstChild.data.strip()
        self._tipp = baum.getElementsByTagName('tipp')[0].firstChild.data.strip()
        self._verwendbarkeit = int(baum.getElementsByTagName('verwendbarkeit')[0].firstChild.data.strip())
        
        
    def verwendet(self):
        self._verwendbarkeit = self._verwendbarkeit - 1
        return self._verwendbarkeit

class maschine(bestand, save):
    
    def lade(self, path):
        
        self._zustaende = []
        self._stand = 0
        
        data.extract('maschinen/'+string.lower(self._name)+'.xml', 'temp')
        
        m_path = os.path.join(path, 'temp/maschinen/'+string.lower(self._name)+'.xml')
        b = dom.parse(m_path)
        os.remove('temp/maschinen/'+string.lower(self._name)+'.xml')
        
        self._name = b.getElementsByTagName('name')[0].firstChild.data.strip()
        self._beschreibung = b.getElementsByTagName('beschreibung')[0].firstChild.data.strip()
        self._werkzeug = b.getElementsByTagName('werkzeug')[0].firstChild.data.strip()
        
        for z in b.getElementsByTagName('zustand'):
            self._zustaende.append(z.firstChild.data.strip())
            if int(z.getAttribute('value')) == 1:
                self._stand = len(self._zustaende) - 1
                        
    def zustand(self):
        return self._zustaende[self._stand]
            
    def verwende(self, was):
        if was == self._werkzeug:
            self._stand = (self._stand + 1)%len(self._zustaende)
            return self._name +' ist nun '+self._zustaende[self._stand]+'.'
        else:
            raise Error, 'Das passt nicht zusammen.'
        
    def beschreibung(self):
        return (self._beschreibung +', '+ self.zustand()+'.').encode('utf-8')

class link(bestand, save):
    
    def __init__(self, data):
        self.reset()
        
        self._richtung = data.getElementsByTagName('richtung')[0].firstChild.data.strip()
        a = data.getElementsByTagName('anker')
        self._anker = a[0].firstChild.data.strip()
        if a[0].getAttribute('bedingung'): self._bedingung = a[0].getAttribute('bedingung')
        self._beschreibung = data.getElementsByTagName('beschreibung')[0].firstChild.data.strip()
        self._ziel = data.getElementsByTagName('ziel')[0].firstChild.data.strip()

        
    def reset(self):
        
        self._richtung = ''
        self._anker = ''
        self._bedingung = ''
        self._ziel = ''
        
    def beschreibung(self, womit):
        if womit != '':
            n = womit.beschreibung()
            nn = string.lower(n[0])+n[1:-1]
            return 'Im '+self._richtung+' '+nn+', '+self._beschreibung+' '+self._ziel+'.'
        else:
            return 'Im '+self._richtung+' '+self._anker+' '+self._beschreibung+' '+self._ziel+'.'
        
    def follow(self):
        
        return self._ziel
        


##-----------------
## TEST
##-----------------
#a = ort('Haus')
#a.lade('/home/olli/Desktop/informatik/10c/objs')
#print a.beschreibung()
